package com.shixi.service;

import com.github.pagehelper.PageHelper;
import com.shixi.dao.OrderDao;
import com.shixi.dao.ProductDao;
import com.shixi.domain.MyOrder;
import com.shixi.domain.Order;
import com.shixi.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService {
    @Autowired
    OrderDao orderDao;
    @Autowired
    ProductDao productDao;

    /*根据卖家id查询用户订单*/
    public List<MyOrder> getUserOrderByUsername(int page, int limit, String username) {
        PageHelper.startPage(page, limit);
        return orderDao.getUserOrderByUsername(username);
    }

    /*根据买家id查询用户订单*/
    public List<MyOrder> getUserOrder(int page, int limit, String username) {
        PageHelper.startPage(page, limit);
        return orderDao.getUserOrder(username);
    }

    /*添加订单*/
    public boolean addOrder(Order order) {
        boolean result = false;
        Product product = productDao.getProductById(order.getOrder_goodsid());
        if (product == null) {
            return false;
        }
        if (Integer.parseInt(product.getGoods_count()) <= 0) {
            return false;
        }

        if (orderDao.addOrder(order.getOrder_id(), order.getOrder_username(), order.getOrder_goodsid(), order.getOrder_time(), order.getOrder_address(), order.getOrder_price()) > 0) {
            productDao.updateProductCount(order.getOrder_goodsid());
            result = true;
        }
        return result;
    }
}
